package estore.model;

import java.io.Serial;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 商品实体类
 */
public class Product implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    private String id;
    private String name;
    private double price;
    private int stock;
    private String description; // 新增描述字段
    private Set<String> tags;

    public Product(String id, String name, double price, int stock) {
        this(id, name, price, stock, "", new HashSet<>());
    }

    public Product(String id, String name, double price, int stock, Set<String> tags) {
        this(id, name, price, stock, "", tags);
    }

    public Product(String id, String name, double price, int stock, String description, Set<String> tags) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
        this.description = (description != null) ? description : "";
        this.tags = (tags != null) ? tags : new HashSet<>();
    }

    // Getters and Setters
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }
    public int getStock() { return stock; }
    public void setStock(int stock) { this.stock = stock; }
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }
    public Set<String> getTags() { return tags; }
    public void setTags(Set<String> tags) { this.tags = tags; }

    @Override
    public String toString() {
        String baseInfo = String.format("ID: %-5s | 名称: %-20s | 价格: ¥%-8.2f | 库存: %d",
                id, name, price, stock);
        if (description != null && !description.isEmpty()) {
            baseInfo += " | 描述: " + description;
        }
        if (tags != null && !tags.isEmpty()) {
            return baseInfo + " | 标签: " + tags;
        } else {
            return baseInfo;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}